package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.ImgUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.PermissionsChecker;
import com.diudiutechnology.www.yadiudiu.util.utils.PictureUtil;
import com.diudiutechnology.www.yadiudiu.util.utils.SelectPhotoPop;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.util.view.UniversalPhotoPop;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.BaseActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyApplication;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MerchantsTypeModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.UniversalModel;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.model.PhotoInfo;

/**
 * Created by Administrator on 2017/9/2.
 * 添加 户型
 */

public class AddFamilyActivity extends BaseActivity implements View.OnClickListener  , Callback.CommonCallback<String>{
    @Bind(R.id.retur)
    LinearLayout retur;

    Context context;
    @Bind(R.id.within)//套内面积
    EditText within;
    @Bind(R.id.add)//图片
    ImageView add;
    @Bind(R.id.family_pictures)//点击图片
    LinearLayout family_pictures;

    @Bind(R.id.family_classification_layout)//添加分类
            LinearLayout family_classification_layout;

    @Bind(R.id.family_classification)//分类文字
            TextView family_classification;

    @Bind(R.id.button)
    Button button;
    private View viewkre;
    private MyProgressDialog mDialog;
    String Judge_interface = "0";//判断哪个接口   0、上传 1、上传图片
    private SelectPhotoPop selectPhotoPop;
    private DisplayImageOptions options;
    private ImageLoader imageLoader;
    String addimg = "";//图片
String id="";//楼盘id
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewkre = View.inflate(this, R.layout.activity_add_family, null);
        setContentView(viewkre);
        ButterKnife.bind(this);
        context = this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        family_pictures.setOnClickListener(this);
        family_classification_layout.setOnClickListener(this);
        button.setOnClickListener(this);
        options = ImgUtils.imageOptions();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));

        if(getIntent()!=null){
            id=getIntent().getStringExtra("id");
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.family_classification_layout://类型选择
                  processdirection();
                break;
            case R.id.family_pictures://添加照片

                    if (!PermissionsChecker.getPermissionsChecker(this, "0", 0)) {
                        if (!PermissionsChecker.getPermissionsChecker(this, "0", 2)) {
                            showSelectPicPop();
                        } else {
                            ToastUtils.show(context, "请检查是否打开储存权限");
                        }
                    } else {
                        ToastUtils.show(context, "请检查是否打开相机权限");
                    }

                break;
                           /* 注释  相机*/
            case R.id.item_popupwindows_camera:
                selectPhotoPop.dismiss();
                GalleryFinal.openCamera(Constant.CAMERA, MyApplication.getInstance().initGalley(Constant.CAMERA, 1), mOnHanlderResultCallback);
                break;
            /* 注释  相册*/
            case R.id.item_popupwindows_Photo:
                selectPhotoPop.dismiss();
                GalleryFinal.openGallerySingle(Constant.GALLERYSINGLE, MyApplication.getInstance().initGalley(Constant.GALLERYSINGLE, 1), mOnHanlderResultCallback);
                break;
            /* 注释  取消*/
            case R.id.item_popupwindows_cancel:
                selectPhotoPop.dismiss();
                break;
            case R.id.button://提交
                if (TextUtils.isEmpty(within.getText().toString().trim())) {//套内面积
                    ToastUtils.show(context, getResources().getString(R.string.please_input));
                    return;
                }
                if (TextUtils.isEmpty(family_classification.getText().toString().trim())) {//分类
                ToastUtils.show(context, getResources().getString(R.string.please_classifi));
                return;
            }
                if (TextUtils.isEmpty(addimg)) {//图片
                    ToastUtils.show(context, getResources().getString(R.string.please_classifiimg));
                    return;
                }
                processdata();
                break;
        }
    }
    //相册
    private void showSelectPicPop() {
        selectPhotoPop = new SelectPhotoPop(this, this);
        selectPhotoPop.showAtLocation(viewkre, Gravity.BOTTOM, 0, 0);
    }
    private GalleryFinal.OnHanlderResultCallback mOnHanlderResultCallback = new GalleryFinal.OnHanlderResultCallback() {
        @Override
        public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList) {
            Bitmap bitmap = PictureUtil.decodeSampledBitmapFromFile(resultList.get(0).getPhotoPath(), Constant.WIDE, Constant.WIDE);
            String conimg = PictureUtil.saveBitmapFile(bitmap, String.valueOf(0));
            processupload(conimg);
        }

        @Override
        public void onHanlderFailure(int requestCode, String errorMsg) {

        }
    };
    //绑定单位
    private void processdirection() {
        List<UniversalModel> unit= new ArrayList<>();
        String[] names = getResources().getStringArray(R.array.model);
        UniversalModel model;
        for (int i=0;i<names.length;i++){
            model=new UniversalModel();
            model.setName(names[i]);
            unit.add(model);
        }
        UniversalPhotoPop typephoto = new UniversalPhotoPop(context, unit, 1);
        typephoto.showAtLocation(viewkre, Gravity.BOTTOM, 0, 0);
        typephoto.setAddresskListener(new UniversalPhotoPop.OnAddressCListener() {
            @Override
            public void onClick(String province, String city, String area) {
                family_classification.setText(province);
            }
        });
    }
    //上传
    private void processdata() {
        if (Sundry.isOpenNetwork(context)) {
            Judge_interface = "0";
            mDialog = new MyProgressDialog(context);
            HttpClient.addhousetype(id,within.getText().toString().trim(), family_classification.getText().toString().trim(),addimg,this);
        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }

    //上传图片
    private void processupload(String upload) {
        if (Sundry.isOpenNetwork(context)) {
            Judge_interface = "1";
            mDialog = new MyProgressDialog(context);
            HttpClient.upload(upload, this);
        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }

    @Override
    public void onSuccess(String result) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        System.out.println("stringmd5result" + result);
        List<MerchantsTypeModel> getnote = JsonTools.getmerchanttype(result);
        if (getnote != null || getnote.size() > 0) {
            String error = getnote.get(0).getError();
            String mssage = getnote.get(0).getMssage();
            if (error.equals("200")) {
                String content = getnote.get(0).getResult();
                if (Judge_interface.equals("0")) {//上传修改
                    finish();
                    ToastUtils.show(context, mssage);
                } else if (Judge_interface.equals("1")) {//图片
                    addimg = content;
                    imageLoader.displayImage(Constant.HTTP + content, add, options);
                }
            } else if (error.equals("105")) {
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                ToastUtils.show(context, mssage);
            } else {
                ToastUtils.show(context, mssage);
            }
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }
}
